from google.appengine.ext import webapp
from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action

class StartingPoint(Room):
    
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        
        # build list of actions
        self.add_action(Choose)
    
    # return description of room.
    def get_description(self):
        return '''
        Welcome to Game of Thrones. You have 10 points, please choose the numbers of different soldiers from below:
        Archers (X1 point), Cavalry (X2 points), Infantry (X3 points).
        '''
    # return image of room
    def get_image(self):
        return 'starting_point.jpg'
         

class StartingPointHandler(BaseHandler):
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return StartingPoint

# define actions for this room

class MainPage(webapp.RequestHandler):
    
    # tell user about game and prompt for name
    def get(self):
        self.session = Session()    # find or create the session
        web_page = render_page('index.html',{"app_welcome":"Welcome to Game of Thrones. You have 10 points, please choose the numbers of different soldiers from below, Archers (X1 point), Cavalry (X2 points), Infantry (X3 points)."})
        self.response.out.write(web_page)
    
    # user wants to play
    # store name in session
    # generate new answer
    # and prompt user for first guess
    def post(self):
        self.session = Session()    # find or create the session
        
        # if user is playing again, then we already
        # have the nickname.  otherwise get it.
        nickname = self.request.get('nickname')
        if nickname == '':
            nickname = self.session['nickname']
        else:
            self.session['nickname'] = nickname
        
        # save random number to guess (e.g. between 0 and 100) in the session in the 'answer' slot:

        # ENTER YOUR CODE HERE.
        # Note: You will need to look up in the python documentation how to create a random number...
        
        web_page_parameters = {
           'app_top_text': "Hi " + nickname + '''
           You have 10 points. Please choose the number of different types of soldiers below:
           ''' + "\n" + "Archers (X1 point)" + "\n" + "Cavalry (X2 points)" + "\n" + "Infantry (X3 points)",
           'app_prompt': 'Guess which number I am thinking of: ',
           'app_field': 'guess'
        }
        web_page = render_page('play.html',web_page_parameters)
        self.response.out.write(web_page)

class GuessHandler(webapp.RequestHandler):
    # this should not happen.
    # if user tries to go here directly we should
    # redirect to welcome page
    def get(self):
        self.redirect("/")
        
    def post(self):
        self.session = Session()    # find or create the session
        
        nickname = self.session['nickname']
        # Find the answer:
        # ENTER YOUR CODE HERE:
        answer = 42 # replace this with something gets the answer from the session...        
        
        # let's get the user's guess
        guess = int(self.request.get('guess'))
        
        # if user got the right answer, congratulate them and go back to home page
        if guess == answer:
            web_page = render_page('winner.html',{"app_welcome":"Congratulations Mr. " + nickname + "! You won!"})
        else: 
            if guess > answer:
                results = "Your guess (" + str(guess) + ") was too high.  Try again..."
            elif guess < answer:
                results = "Your guess (" + str(guess) + ") was too low.  Try again..."
            else:
                results = "You got it! Try again?"
            web_page_parameters = {
               'app_top_text': results,
               'app_prompt': 'Guess which number I am thinking of: ',
               'app_field': 'guess'
            }
            web_page = render_page('play.html',web_page_parameters)

        self.response.out.write(web_page)

application = webapp.WSGIApplication([
                                      ('/play',GuessHandler),
                                      ('/.*', MainPage)],
                                     debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

class Choose(Action):
    def __init__(self):
        Action.__init__(self, "Choose")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Choose and Submit"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'entrancehall'
    
    def choose_soldier(self,archer,cavalry,infantry):
        return
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "post"